import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:hbtrans_flutter/beans/home_list_bean.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';
import 'package:hbtrans_flutter/view/loading.dart';

import '../../apis.dart';
import '../../const.dart';
import '../../routes.dart';

class Page extends StatefulWidget {
  final labelId;

  const Page({Key key, this.labelId}) : super(key: key);

  @override
  _PageState createState() => _PageState();
}

class _PageState extends State<Page> {
  var loadingDialog;
  var items = List();

  _showDialog() {
    loadingDialog = LoadingDialog(
      text: '请稍候...',
    );
    showDialog<Null>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return loadingDialog;
        });
  }

  _articleList() {
    Map data = Map<String, dynamic>();
    data["labelId"] = widget.labelId;
    MyDio.getInstance(url: Const.TRANS_SERVER_MY_PATH).get(Apis.articleList,
        (data) {
      if (loadingDialog != null) {
        loadingDialog = null;
        Navigator.pop(context);
      }

      setState(() {
        for (var s in data) {
          items.add(ArticlesListBean.fromJson(s));
        }
      });
    }, params: data);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _articleList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView(
        padding: EdgeInsets.all(0), //解决顶部导航栏宽度无法置顶
        children: _doWidgets(),
      ),
    );
  }

  List<Widget> _doWidgets() {
    List<Widget> listTitles = List();
    for (var i = 0; i < items.length; i++) {
      listTitles.add(buildListItem(context, items[i]));
    }
    return listTitles;
  }

  Widget buildListItem(BuildContext context, ArticlesListBean bean) {
    return GestureDetector(
      onTap: () {
        String route = '${Routes.articleDetail}?articleId=${bean.id}';

        Routes.router.navigateTo(context, route,
            replace: false, transition: TransitionType.fadeIn);
      },
      child: SizedBox(
        height: 80,
        width: 355,
        child: Card(
          elevation: 0,
          color: Colors.white,
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 6),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Image.network(
                  bean.headPic,
                  height: 60,
                  width: 80,
                  fit: BoxFit.fill,
                ),
                SizedBox(
                  width: 206,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: <Widget>[
                      Text(
                        bean.title,
                        maxLines: 2,
                        softWrap: true,
                        style: TextStyle(
                            fontSize: 16, color: const Color(0xff4A4A4A)),
                      ),
                      Text(
                        bean.createTime,
                        style: TextStyle(
                            fontSize: 12, color: const Color(0xffB2B2B2)),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
